//
//  TweetDetailViewModel.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/1.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCTweetDetailViewModel.h"
#import "OSCTweetTools.h"
#import "OSCCommentTools.h"
#import "OSCTweetCommentViewModel.h"
#import "OSCCommentRequestParams.h"
@interface OSCTweetDetailViewModel ()
/// 请求评论参数
@property (nonatomic, strong) OSCCommentRequestParams *listParams;

@end

@implementation OSCTweetDetailViewModel

@dynamic listParams;

- (instancetype)initWithServices:(id<OSCViewModelServices>)services params:(NSDictionary *)params {
    self = [super initWithServices:services params:params];
    if (self) {
        self.tweetViewModel = [params valueForKey:@"tweetViewModel"];
        self.listParams = [[OSCCommentRequestParams alloc] initWithIdField:self.tweetViewModel.idField Catalog:OSCCommentCatalogTweets];
    }
    return self;
}
- (void)initialize {
    [super initialize];
    
    self.dataSourceSignal = [self.requestListDataCommand.executionSignals.flatten map:^id(NSArray *models) {
        NSMutableArray *cellViewModels = [[NSMutableArray alloc] init];
        for (OSCComment *model in models) {
            OSCTweetCommentViewModel *cellViewModel = [[OSCTweetCommentViewModel alloc] initWithCommentModel:model];
            [cellViewModels addObject:cellViewModel];
        }
        return cellViewModels;
    }];
    
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(OSCTweetCommentViewModel *viewModel) {
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:viewModel];
            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] takeUntil:self.rac_willDeallocSignal];
    }];
    
    [self.requestListDataCommand.errors subscribe:self.errors];
    
}

- (RACSignal *)requestListDataSignal {
    return [OSCCommentTools requestCommentWithParamsModel:self.listParams];
}

@end
